import { createRouter, createWebHistory } from "vue-router"
import { useLoginGuard } from "@/guard/loginGuard"

const isLogin = useLoginGuard()
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "indexView",
      component: () => import("@/views/IndexView.vue"),
      children: [
        {
          path: "",
          name: "index",
          component: () => import("@/pages/index/index/Index.vue")
        },
        {
          path: "/market",
          name: "market",
          component: () => import("@/pages/index/market/Market.vue")
        },
        {
          path: "/login",
          name: "loginView",
          component: () => import("@/views/LoginView.vue"),
          children: [
            {
              path: "/login/admin",
              name: "login-admin",
              component: () => import("@/pages/index/login/AdministratorLogin.vue")
            },
            {
              path: "/login/admin/forget",
              name: "login-admin-forget",
              component: () => import("@/pages/index/login/AdminForget.vue")
            },
            {
              path: "/login/user",
              name: "login-user",
              component: () => import("@/pages/index/login/UserLogin.vue")
            },
            {
              path: "/login/user/register",
              name: "login-user-register",
              component: () => import("@/pages/index/login/UserRegister.vue")
            },
            {
              path: "/login/user/forget",
              name: "login-user-forget",
              component: () => import("@/pages/index/login/UserForget.vue")
            }
          ]
        }
      ]
    },

    {
      path: "/admin",
      name: "admin",
      component: () => import("@/views/AdminView.vue"),
      beforeEnter(to: any, from: any, next: any) {
        if (isLogin) {
          next()
        } else {
          next("/login/admin")
        }
      },
      meta: { title: "首页" },
      children: [
        {
          path: "/admin/",
          name: "admin-dashboard",
          component: () => import("@/pages/admin/dashboard/Dashboard.vue"),
          meta: { title: "数据面板" }
        },
        {
          path: "/admin/program",
          name: "admin-program",
          component: () => import("@/pages/admin/program/Program.vue"),
          meta: { title: "应用管理" }
        },
        {
          path: "/admin/user",
          name: "admin-user",
          component: () => import("@/pages/admin/user/User.vue"),
          meta: { title: "用户管理" }
        },
        {
          path: "/admin/recycle/program",
          name: "admin-recycle-program",
          component: () => import("@/pages/admin/recycle/program/Program.vue"),
          meta: { title: "回收站-应用管理" }
        },
        {
          path: "/admin/authorization",
          name: "admin-authorization",
          component: () => import("@/pages/admin/authorization/Authorization.vue"),
          meta: { title: "授权管理" }
        },
        {
          path: "/admin/recycle/authorization",
          name: "admin-recycle-authorization",
          component: () => import("@/pages/admin/recycle/authorization/Authorization.vue"),
          meta: { title: "回收站-授权管理" }
        },
        {
          path: "/admin/recycle/order",
          name: "admin-recycle-order",
          component: () => import("@/pages/admin/recycle/order/Order.vue"),
          meta: { title: "回收站-支付订单" }
        },
        {
          path: "/admin/piracy",
          name: "admin-piracy",
          component: () => import("@/pages/admin/piracy/Piracy.vue"),
          meta: { title: "盗版记录" }
        },
        {
          path: "/admin/order",
          name: "admin-order",
          component: () => import("@/pages/admin/order/Order.vue"),
          meta: { title: "查看订单" }
        },
        {
          path: "/admin/pay",
          name: "admin-pay",
          component: () => import("@/pages/admin/pay/Pay.vue"),
          meta: { title: "支付设置" }
        },
        {
          path: "/admin/copyright",
          name: "admin-copyright",
          component: () => import("@/pages/admin/copyright/Copyright.vue"),
          meta: { title: "版权设置" }
        },
        {
          path: "/admin/myself",
          name: "admin-myself",
          component: () => import("@/pages/admin/myself/Myself.vue"),
          meta: { title: "个人中心" }
        },
        {
          path: "/admin/email",
          name: "admin-email",
          component: () => import("@/pages/admin/email/Email.vue"),
          meta: { title: "邮件配置" }
        }
      ]
    },
    {
      path: "/user",
      name: "user",
      component: () => import("@/views/UserView.vue"),
      beforeEnter(to: any, from: any, next: any) {
        if (isLogin) {
          next()
        } else {
          next("/login/user")
        }
      },
      meta: { title: "首页" },
      children: [
        {
          path: "/user/authorization",
          name: "user-authorization",
          component: () => import("@/pages/user/authorization/Authorization.vue"),
          meta: { title: "授权管理" }
        }
      ]
    },
    {
      path: "/:pathMatch(.*)",
      name: "404", //404页面
      component: () => import("@/views/404View.vue")
    },
  ]
})
export default router
